<?php
/**
 * Created by PhpStorm.
 * User: zhengzifan
 * Date: 2017/3/16
 * Time: 14:18
 */

namespace app\admin\controller;
use think\Controller;
use think\Verify;
class PublicController extends Controller{

    /**
     * 登录页
     */
    public function login(){
        $system_config=db('System')->where(['id'=>['>','0']])->find();
        if(IS_POST){
            $adminModel = model('Admin');
			if($adminModel->check_verify()){
				$admin=$adminModel->where('username',input('username'))->find();
				if($admin){
					$where = [
						'a.username' => input('username'),
						'a.password' => md5(input('password')),
					];
					$admin = $adminModel
						->alias('a')
						->join('__RBAC_ROLE_ADMIN__ rr', 'rr.admin_id=a.id')
						->field('a.*,rr.role_id')
						->where($where)
						->find();
					if($admin){
						$adminModel->update(['lasttime' => time(),'IP' => $_SERVER['REMOTE_ADDR'],'id'=>$admin['id']]);
						session('admin',$admin);
						admin_log('后台登录');
						$this->redirect('Index/index');
					}else{
						getAlert('密码错误');
					}
				}else{
					getAlert('账号不存在');
				}
			}else{
				getAlert('验证码不正确');
			}
        }
        $this->assign(['domain'=>$this->request->url(true),'system_config'=>$system_config]);
        return $this->fetch('login');
    }

    public function logout(){
        admin_log('后台退出登录');
        session('admin',null);
        $this->redirect('Public/login');
    }

    public function verify(){
		ob_clean();
        $config = [
            'length' => 4,
            'imageH' => 34,
            'imageW' => 140,
            'fontSize' => 18,
		];
		header('Content-Type:image/png');
        $verify = new Verify($config);
        $verify->entry();
    }
}